using System;

namespace Mbs.CastleContainer
{
    public class KernelListnerManager : ListnerManager<IKernelListner,KernelListnerType>, IKernelListnerManager
    {
        public IKernelListnerManager Parent { get; set; }
        public KernelListnerType Type { get; private set; }

        #region IKernelListner Members
        public void OnAddedAsChildKernel(object sender, EventArgs e)
        {
            Foreach((item) => item.OnAddedAsChildKernel(sender, e));

            if (Parent != null)
                Parent.OnAddedAsChildKernel(sender, e);
        }

        public void OnComponentCreated(Castle.Core.ComponentModel model, object instance)
        {
            Foreach((item) => item.OnComponentCreated(model, instance));
            if (Parent != null)
                Parent.OnComponentCreated(model, instance);
        }

        public void OnComponentDestroyed(Castle.Core.ComponentModel model, object instance)
        {
            Foreach((item) => item.OnComponentDestroyed(model, instance));
            if (Parent != null)
                Parent.OnComponentDestroyed(model, instance);
        }

        public void OnComponentModelCreated(Castle.Core.ComponentModel model)
        {
            Foreach((item) => item.OnComponentModelCreated(model));
            if (Parent != null)
                Parent.OnComponentModelCreated(model);
        }

        public void OnComponentRegistered(string key, Castle.MicroKernel.IHandler handler)
        {
            Foreach((item) => item.OnComponentRegistered(key, handler));
            if (Parent != null)
                Parent.OnComponentRegistered(key, handler);
        }

        public void OnComponentUnregistered(string key, Castle.MicroKernel.IHandler handler)
        {
            Foreach((item) => item.OnComponentUnregistered(key, handler));
            if (Parent != null)
                Parent.OnComponentUnregistered(key, handler);
        }

        public void OnDependencyResolving(Castle.Core.ComponentModel client, Castle.Core.DependencyModel model, object dependency)
        {
            Foreach((item) => item.OnDependencyResolving(client, model, dependency));
            if (Parent != null)
                Parent.OnDependencyResolving(client, model, dependency);
        }

        public void OnHandlerRegistered(Castle.MicroKernel.IHandler handler, ref bool stateChanged)
        {
            if (Count > 0)
                foreach (var item in this)
                    if (item != null && ((item.Type & KernelListnerType.HandlerRegistered) == KernelListnerType.HandlerRegistered)) 
                        item.OnHandlerRegistered(handler, ref stateChanged);

            if (Parent != null)
                Parent.OnHandlerRegistered(handler, ref stateChanged);
        }

        public void OnRemovedAsChildKernel(object sender, EventArgs e)
        {
            Foreach((item) => item.OnRemovedAsChildKernel(sender, e));
            if (Parent != null)
                Parent.OnRemovedAsChildKernel(sender, e);
        }

        #endregion
    }
}
